package org.tuoren.official.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.tuoren.common.core.utils.MapstructUtils;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.tuoren.official.domain.bo.CarouselBo;
import org.tuoren.official.domain.vo.CarouselVo;
import org.tuoren.official.domain.Carousel;
import org.tuoren.official.mapper.CarouselMapper;
import org.tuoren.official.service.ICarouselService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 轮播图Service业务层处理
 *
 * @author wl
 * @date 2024-11-26
 */
@RequiredArgsConstructor
@Service
public class CarouselServiceImpl implements ICarouselService {

    private final CarouselMapper baseMapper;

    /**
     * 查询轮播图
     *
     * @param id 主键
     * @return 轮播图
     */
    @Override
    public CarouselVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询轮播图列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 轮播图分页列表
     */
    @Override
    public TableDataInfo<CarouselVo> queryPageList(CarouselBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Carousel> lqw = buildQueryWrapper(bo);
        Page<CarouselVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    @Override
    public TableDataInfo<CarouselVo> frontList() {
        LambdaQueryWrapper<Carousel> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Carousel::getPictureOrder);
        lqw.eq(Carousel::getIsShow,1);
        return TableDataInfo.build(baseMapper.selectVoList(lqw));
    }
    /**
     * 查询符合条件的轮播图列表
     *
     * @param bo 查询条件
     * @return 轮播图列表
     */
    @Override
    public List<CarouselVo> queryList(CarouselBo bo) {
        LambdaQueryWrapper<Carousel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Carousel> buildQueryWrapper(CarouselBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Carousel> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoUrl()), Carousel::getPhotoUrl, bo.getPhotoUrl());
        lqw.eq(bo.getIsShow()!=null, Carousel::getIsShow, bo.getIsShow());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoUrl()), Carousel::getVideoUrl, bo.getVideoUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getTextDesc()), Carousel::getTextDesc, bo.getTextDesc());
        lqw.eq(bo.getPictureOrder() != null, Carousel::getPictureOrder, bo.getPictureOrder());
        return lqw;
    }

    /**
     * 新增轮播图
     *
     * @param bo 轮播图
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CarouselBo bo) {
        Carousel add = MapstructUtils.convert(bo, Carousel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改轮播图
     *
     * @param bo 轮播图
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CarouselBo bo) {
        Carousel update = MapstructUtils.convert(bo, Carousel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Carousel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除轮播图信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
